#include<IRremote.h>
int commandnum = 2;
int ledPin = 10;
int RECV_PIN = 11;
int buttonPin = 6;
int relayPin = 7;
boolean relayState = LOW;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 10;
boolean ledState = LOW;
IRrecv irrecv(RECV_PIN);
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  irrecv.enableIRIn();//也可以irrecv.start()
  pinMode(ledPin,OUTPUT);
  pinMode(buttonPin,INPUT);
  pinMode(relayPin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(buttonPin) == HIGH){
    buttonActRelay();
  }
  if(irrecv.decode()){
    Serial.println(irrecv.decodedIRData.decodedRawData,HEX);
    IRremoteActRelay(irrecv.decodedIRData.decodedRawData);
    irrecv.resume();
  }
}
void buttonActRelay(){
  int reading = digitalRead(buttonPin);
  if(reading != lastButtonState){
    lastDebounceTime = millis();
  }
  if((millis()-debounceDelay)>debounceDelay){
    if(reading!=buttonState){
      buttonState = reading;

      if(buttonState == HIGH){
        relayState = !relayState;
      }
    }
    digitalWrite(relayPin,relayState);

    lastButtonState = reading;
  }
}
void IRremoteActRelay(long RawData){
  long command[commandnum] = {0xF30CFF00 , 0xE718FF00 };
  Serial.println(RawData);
    if(RawData == command[0]){
      ledState = !ledState;
      digitalWrite(ledPin,ledState);
    }
    if(RawData == command[1]){
      relayState = !relayState;
      digitalWrite(relayPin,relayState);
    }
}
